/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package MVC;

import ChatNI.*;
import MVC.*;
import InterfaceUser.*;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.Arrays;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

/*
 * EcranConnexion
 * The first interface with whom the user interacts
 * The window connection for connecting to the ChatSystem
 * @author Duy Duc, Metangmo
 */
	
public class EcranConnexion extends JFrame implements ActionListener, WindowListener ,FromUser{
	
    protected JLabel lBienvenu ;

    protected JLabel lVide1 ; 

    protected JButton bConnect ;
    
    protected JButton bCancel ;

    protected JLabel lpseudo ;

    protected JTextField textPseudo ;
    
    public ChatGUI chat;

    private ChatController controller;
    
    
    /**
     * Constructor
     * @param controller
     *        Instance de ChatController
     */
    public EcranConnexion(ChatController controller ){
            initComponents() ;
            this.controller = controller;
    }
    
    
    /**
     * initComponents:
     * function to initialize the necessary parameters of the window connection
     */
    private void initComponents(){
            //Titre
            this.setTitle("Connection Window");

            GridLayout l = new GridLayout(4,2) ;
            this.setLayout(l) ;

            // position in the center of the screen
            this.setLocationRelativeTo(this.getParent());

            // Size modification
            this.setSize(100,100);
                               
            
            //all the attributes
            bConnect = new JButton("Connect") ;
            bCancel = new JButton("Quit");
            lpseudo = new JLabel("Your login :",0);
            textPseudo = new JTextField("");
            lBienvenu = new JLabel("Welcome to the ChatSystem Connection Window",0);	
            lVide1 = new JLabel("") ;  
            
            //The attributes are added in the JFrame
            
            this.add(lBienvenu);
            this.add(lVide1);
            
            this.add(lpseudo);            
            this.add(textPseudo);
            
            this.add(bConnect);
            this.add(bCancel);
            
            //The button bConnect is selected by default ( You can press the Enter key)
            this.getRootPane().setDefaultButton(bConnect) ;
            this.pack() ;
            this.setVisible(true);
            
            //ActionListener is linked with two buttons
            bConnect.addActionListener(this);
            bCancel.addActionListener(this);
            
            addWindowListener(this);

    }
    
    /**
     * actionPerformed:
     * function to realize the actions according to the Connect button and the Cancel button
     * Connect button:
     * it verifies the given password by comparing with the correct one
     * if the password is correct, the connection window is replaced by the chat window
     * if not, a message is displayed to report about the incorrect password
     * Cancel button:
     * The connection window is closed. The program stops
     * @param e 
     *        ActionEvent
     */
    @Override
    public void actionPerformed(ActionEvent e) {
            //Button bConnect is pressed
            if (e.getSource() == bConnect){
                
                
                    
                    this.connect(textPseudo.getText());

                try {
                    // Hello-True is sent in broadcast
                    controller.performConnect(textPseudo.getText());
                    System.out.println("TextPseudo =" + textPseudo.getText());
                    
                } catch (UnknownHostException ex) {
                    Logger.getLogger(EcranConnexion.class.getName()).log(Level.SEVERE, null, ex);
                } catch (SocketException ex) {
                    Logger.getLogger(EcranConnexion.class.getName()).log(Level.SEVERE, null, ex);
                }
                                        
            } 
     
            //Button bCancel is pressed
            else if (e.getSource() == bCancel){
                System.exit(0);
            }
    }

    
       @Override
        public void windowOpened(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
        /**
         * windowClosing
         * function closes the ConnectionWindow and stop completely the ChatSystem
         * @param we 
         *        WindowEvent  
         */
        @Override
        public void windowClosing(WindowEvent we) {
            System.out.println("ConnectionWindow is closed");
            System.exit(0);
        }

        @Override
        public void windowClosed(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void windowIconified(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void windowDeiconified(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void windowActivated(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void windowDeactivated(WindowEvent we) {
            //throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
        
        /**
         * connect:
         * function to perform the initialization of the phase connection by adding the components of the MVC
         * The window connection will be closed and replaced by the general window chat
         * @param username
         *        Nickname of the LocalUser
         */
        
        @Override
        public void connect(String username) {

            //The active window is closed and replaced by another one
            dispose();

            //The button bConnection is called leads to the opening of this window 
            chat = new ChatGUI(username,controller );

             // The ChatGUI just created is associated to the ChatController
             controller.addView(chat);

             // The observer of the ModelUser and the ModelMessIn is ajouted
             controller.getModele().addObserver(chat);
             controller.getModeleMessIn().addObserver(chat);


        }

        @Override
        public void disconnect() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }


        @Override
        public void selectUser() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void sendMessage() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }

        @Override
        public void selectFile() {
            throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
        }
        
}
